﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Management_Nurse_PatientMedical : System.Web.UI.UserControl
{
    static int currentPatient;
    static int currentAppointment;

    public void loadInfoPatient(int IDPatient, int IDAppointment)
    {
        currentPatient = IDPatient;
        currentAppointment = IDAppointment;

        String sql = "SELECT [ID],[FirstName] + ' ' + [LastName] AS [Name],[Sex],[DOB],[Address], [tbImpatient].[IDBed], [tbImpatient].[IDWard] "
                    + " FROM [tbPatient],[tbImpatient]"
                    + " WHERE [tbImpatient].[IDPatient] = [tbPatient].[ID] AND [tbPatient].[ID] = " + IDPatient;
        SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        connection.Open();
        SqlCommand cmd = new SqlCommand(sql, connection);
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        tbID.Text = reader.GetInt32(0).ToString();
        tbName.Text = reader.GetString(1);
        if (reader.GetInt32(2) == 0)
        {
            tbGender.Text = "Nữ";
        }
        else
        {
            tbGender.Text = "Nam";
        }
        DateTime t = reader.GetDateTime(3);
        tbDob.Text = String.Format("{0:dd-MM-yyyy}", t);
        Label1.Text = reader.GetString(4);
        tbIDBed.Text = reader.GetInt32(5).ToString();
        int IDWard = reader.GetInt32(6);
        connection.Close();

        //================= Load List Drup of ward=======================//
        lbWard.Text = IDWard.ToString();

        loadListDrugOfWard(IDWard);        
        showDrugUsed(IDPatient,IDAppointment);
        
    }

    private void showDrugUsed(int IDPatient, int IDAppointment)
    {
        thuocDS.SelectParameters["IDPatient"].DefaultValue = IDPatient.ToString();
        thuocDS.SelectParameters["IDAppointment"].DefaultValue = IDAppointment.ToString();
        GridView1.DataBind();
    }

    private void loadListDrugOfWard(int IDWard)
    {
        DanhSachThuoc1.loadTypeByIDWard(IDWard);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        conn.Open();
        SqlCommand cmd;
        // insert into a new line tbUseDrug 
        DateTime sDay = dateStart.getDateTime();
        DateTime eDay = dateEnd.getDateTime();
        int unitByDay = int.Parse(tbUnitbyDay.Text);
        if ( unitByDay <= 0)
        {            
            Response.Write("<script>alert('Nhập liều dùng mỗi ngày')</script>");
            return;
        }
        if (eDay < sDay)
        {
            Response.Write("<script>alert('Ngày sử dụng không hợp lệ')</script>");
            return;
        }
       
        // Kiểm tra đủ số lượng thuốc sử dụng hay không?
        int numberDay = (int)((eDay - sDay).TotalDays) + 1;

        string sqlCheck = "SELECT [Quantity] FROM [tbTotalDrug] WHERE [IDDrug] = " + DanhSachThuoc1.SelectedID;
        cmd = new SqlCommand(sqlCheck, conn);
        int quantity = (int) cmd.ExecuteScalar();

        if (numberDay * unitByDay > quantity)
        {
            Response.Write("<script>alert('Số lượng thuốc không đủ')</script>");
            return;
        }
        quantity -= numberDay * unitByDay;


        string sqlInsert = "INSERT INTO [tbUseDrug] VALUES (@IDPatient,@IDAppointment,@IDDrug,@UnitPerDay,@sDay,@eDay)";
        cmd = new SqlCommand(sqlInsert, conn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("IDPatient", currentPatient);
        cmd.Parameters.AddWithValue("IDAppointment", currentAppointment);
        cmd.Parameters.AddWithValue("IDDrug", DanhSachThuoc1.SelectedID);
        cmd.Parameters.AddWithValue("UnitPerDay", tbUnitbyDay.Text);
        cmd.Parameters.AddWithValue("sDay", sDay);
        cmd.Parameters.AddWithValue("eDay", eDay);
        cmd.ExecuteNonQuery();

        // Update số lượng thuốc đã dùng

        string sqlUpdate = "UPDATE [tbTotalDrug] SET [Quantity] = @quantity WHERE IDDrug = " + DanhSachThuoc1.SelectedID;
        cmd = new SqlCommand(sqlUpdate, conn);
        cmd.Parameters.AddWithValue("quantity", quantity);
        cmd.ExecuteNonQuery();


        conn.Close();
        GridView1.DataBind();

        // Clear Text
        tbUnitbyDay.Text = "";
    }
}